﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Shop.Data;

namespace Shop.CartResources
{
    public partial class Payment : Form
    {
        private IEnumerable<ICartItem> cartItems;
        private bool finished;

        public Payment(Cart currentCart)
        {
            InitializeComponent();
            this.cartItems = currentCart.CartItems;
            this.radLabelFullPrice.Text = currentCart.FullCartPrice.ToString();
            this.finished = false;
        }

        private void radButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radButtonSave_Click(object sender, EventArgs e)
        {
            decimal givenSum = 0;
            if (decimal.TryParse(this.radTextBoxGivenCash.Text, out givenSum))
            {
                SaveReport(givenSum);
            }
            else
            {
                MessageBox.Show("Грешно въведена сума в брой");
            }
            if (finished)
            {
                foreach (ICartItem currentCartItem in cartItems)
                {
                    Shop.Data.ShopData.UpdateShopItem(currentCartItem.Barcode,currentCartItem.Price);
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void SaveReport(decimal givenSum)
        {
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            object oMissing = System.Reflection.Missing.Value;
            decimal fullPrice;
            Document oDoc = CreateDocument(oWord, givenSum, oMissing, out fullPrice, out oDoc);

            if (givenSum >= fullPrice)
            {
                this.saveFileDialog.ShowDialog();
                if (this.saveFileDialog.FileName == string.Empty)
                {
                    MessageBox.Show("Не сте избрали файл");
                }
                else
                {
                    object fileName = this.saveFileDialog.FileName;
                    oDoc.SaveAs(ref fileName,
                        ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing);
                    finished = true;
                }
            }
            else
            {
                MessageBox.Show("Прекалено малка сума. Не можете да платите сметката.");
            }
            oDoc.Close(WdSaveOptions.wdDoNotSaveChanges, ref oMissing, ref oMissing);
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            oWord = null;
        }

        private Document CreateDocument(Microsoft.Office.Interop.Word.Application oWord, decimal givenSum, object oMissing, out decimal fullPrice, out Document oDoc)
        {
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                                                ref oMissing, ref oMissing);
            Paragraph oPara1 = oDoc.Paragraphs.Add(ref oMissing);
            object styleHeading1 = "Heading 1";
            oPara1.Range.set_Style(ref styleHeading1);
            oPara1.Range.Text = "Касова бележка";
            oPara1.Range.InsertParagraphAfter();

            Paragraph separatorOne = oDoc.Paragraphs.Add(ref oMissing);
            separatorOne.Range.Text = new string('-', 30);
            separatorOne.Range.InsertParagraphAfter();

            fullPrice = 0;
            foreach (ICartItem item in this.cartItems)
            {
                Paragraph oPara2 = oDoc.Paragraphs.Add(ref oMissing);
                oPara2.Range.Text = String.Format(" {0}, {1}, {2} лв.", item.Barcode, item.Specification, item.Price);
                oPara2.Range.InsertParagraphAfter();
                fullPrice += item.Price;
            }

            Paragraph separatorTwo = oDoc.Paragraphs.Add(ref oMissing);
            separatorTwo.Range.Text = new string('-', 30);
            separatorTwo.Range.InsertParagraphAfter();

            Paragraph oPara3 = oDoc.Paragraphs.Add(ref oMissing);
            oPara3.Range.Text = String.Format("Общо цена: {0}", fullPrice);
            oPara3.Range.InsertParagraphAfter();

            Paragraph oPara4 = oDoc.Paragraphs.Add(ref oMissing);
            oPara4.Range.Text = String.Format("В брой: {0}", givenSum);
            oPara4.Range.InsertParagraphAfter();

            Paragraph oPara5 = oDoc.Paragraphs.Add(ref oMissing);
            oPara5.Range.Text = String.Format("Ресто: {0}", givenSum - fullPrice);
            oPara5.Range.InsertParagraphAfter();
            return oDoc;
        }
    }
}
